package com.mall.shopping.services;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Deng Zaojian
 * @program: micro-mall
 * @description: 商品模块的查询商品
 * @create: 2021-09-29 10:05
 **/
@Slf4j
@Service
@Component
public class ProductServiceImpl implements IProductService {


    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ProductConverter productConverter;
    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ContentConverter contentConverter;


    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        Long id = request.getId();

        //用于封装返回的数据
        ProductDetailResponse productDetailResponse = new ProductDetailResponse();
        ProductDetailDto productDetailDto = new ProductDetailDto();

        //查询具体的商品

        try {


            //先查询商品详细信息表
            Example example = new Example(ItemDesc.class);
            example.createCriteria().andEqualTo("itemId", id);

            List<ItemDesc> itemDescs = itemDescMapper.selectByExample(example);
            if (itemDescs.size() > 0) {
                productDetailDto.setDetail(itemDescs.get(0).getItemDesc());
            }

            //再查询商品表
            Example example1 = new Example(Item.class);
            example1.createCriteria().andEqualTo("id", id);

            List<Item> items = itemMapper.selectByExample(example1);
            if (items.size() > 0) {
                Item item = items.get(0);
                productDetailDto.setProductId(id);
                productDetailDto.setSalePrice(item.getPrice());
                productDetailDto.setProductName(item.getTitle());
                productDetailDto.setSubTitle(item.getSellPoint());
                productDetailDto.setLimitNum(Long.parseLong(item.getLimitNum() + ""));
                productDetailDto.setProductImageBig(item.getImageBig());
                String[] images = item.getImages();
                ArrayList<String> list = new ArrayList<>();
                for (String image : images) {
                    list.add(image);
                }
                productDetailDto.setProductImageSmall(list);

            }
            if (items.size() > 0) {
                productDetailResponse.setProductDetailDto(productDetailDto);
                productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
                productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            } else {
                productDetailResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
                productDetailResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());

            }
        } catch (Exception e) {
            log.error("ProductServiceImpl.getProductDetail occur Exception: " + e);
            ExceptionProcessorUtils.wrapperHandlerException(productDetailResponse, e);

        }
        return productDetailResponse;
    }


    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse allProductResponse = new AllProductResponse();
        request.requestCheck();

        try {
            //取出传入的参数
            Integer page = request.getPage();
            Integer size = request.getSize();
            Long cid = null;
            Integer priceGt = null;
            Integer priceLte = null;
            if (request.getCid()!=null && !"".equals(request.getCid())){
                
             cid = request.getCid();
            }
            if (request.getPriceGt()!=null && !"".equals(request.getPriceGt())){
                priceGt = request.getPriceGt();
                if (priceGt<0){
                    priceGt=0;
                }
            }
            if (request.getPriceLte()!=null && !"".equals(request.getPriceLte())){
                priceLte = request.getPriceLte();
                if (priceLte <0){
                    priceLte=0;
                }
            }
            
            
//            Integer priceGt = request.getPriceGt();
//            Integer priceLte = request.getPriceLte();
            String sort = request.getSort();

            //排序列
            String orderCol = null;

            //排序,1 从低到高  -1从高到低, null 综合排序
            String orderDir = null;
            if (sort == null) {
                orderCol = "created asc";
                orderDir = ",price desc";
            } else {

                if (Integer.valueOf(sort) == -1) {
                    orderCol = "price";
                    orderDir = "desc";
                } else if (Integer.valueOf(sort) == 1) {
                    orderCol = "price";
                    orderDir = "asc";
                }
            }


            List<Item> items = itemMapper.selectItemFront(cid, orderCol, orderDir, priceGt, priceLte);
            List<ProductDto> productDtos = productConverter.items2Dto(items);

            PageHelper.startPage(page, size);
            PageInfo<ProductDto> pageInfo = new PageInfo<>(productDtos);

            //封装
            allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            allProductResponse.setData(productDtos);
            allProductResponse.setTotal(pageInfo.getTotal());
/*            if (items.size() > 0) {
            } else {
                allProductResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
                allProductResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
                allProductResponse.setData(null);
                allProductResponse.setTotal(0L);
            }*/

        } catch (Exception e) {
            log.error("ProductServiceImpl.getAllProduct occur Exception: " + e);
            ExceptionProcessorUtils.wrapperHandlerException(allProductResponse, e);

        }

        return allProductResponse;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse recommendResponse = new RecommendResponse();

        try {


            //拿到板块表内容和关联的商品id
            List<Panel> panels = panelMapper.selectPanelContentById(6);


            Set<PanelDto> set = new HashSet<>();
            PanelDto panelDto = new PanelDto();

            if (panels.size() > 0) {
                Panel panel = panels.get(0);
                panelDto = contentConverter.panen2Dto(panel);

            }

            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(6);
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            panelDto.setPanelContentItems(panelContentItemDtos);

            if (panels.size() > 0) {
                set.add(panelDto);
                recommendResponse.setPanelContentItemDtos(set);
                recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
                recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            } else {
                recommendResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
                recommendResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());

            }

        } catch (Exception e) {
            log.error("ProductServiceImpl.getRecommendGoods occur Exception: " + e);
            ExceptionProcessorUtils.wrapperHandlerException(recommendResponse, e);

        }
        return recommendResponse;
    }
}
